﻿using System;
using System.Collections.Generic;
using System.IO;
using SimpleFS.Core;

[assembly: CLSCompliant(true)]
namespace SimpleFS.Storage.Windows
{
    public class Directory : IDirectory
    {
        internal string location = string.Empty;

        public Directory(string location)
        {
            this.location = location;
        }

        public void Delete()
        {
            System.IO.Directory.Delete(this.location);
        }

        public bool Exists()
        {
            return System.IO.Directory.Exists(this.location);
        }

        public void Rename(string newName)
        {
            Console.WriteLine("old name: {0}", location);
            Console.WriteLine("file name: {0}", newName);
            System.IO.Directory.Move(this.location, newName);
            this.location = newName;
        }

        #region IDirectory Members

        public IDirectory[] Directories
        {
            get
            {
                string[] subDirectoryNames = 
                    System.IO.Directory.GetDirectories(this.location);
                IDirectory[] subDirectories 
                    = new Directory[subDirectoryNames.Length];

                for (int index = 0; index < subDirectoryNames.Length; index++)
                {
                    subDirectories[index] = new Directory(subDirectoryNames[index]);
                }

                return subDirectories;
            }
        }

        public IFile[] Files
        {
            get
            {
                string[] fileNames = System.IO.Directory.GetFiles(this.location);
                IFile[] files = new File[fileNames.Length];

                for (int index = 0; index < fileNames.Length; index++)
                {
                    files[index] = new File(fileNames[index]);
                }

                return files;
            }
        }

        public IDirectory GetDirectory(string directoryName)
        {
            string[] directories = null;

            try
            {
                directories = System.IO.Directory.GetDirectories(directoryName);
            }
            catch(Exception ex)
            {
                return null;
            }
            Directory directroy = new Directory(directories[0]);

            return directroy;
        }

        public IFile GetFile(string fileName)
        {
            string[] files;

            try
            {
                files = System.IO.Directory.GetFiles(fileName);
            }
            catch(Exception ex)
            {
                return null;
            }
            File file = new File(files[0]);

            return file;
        }

        #endregion

        #region IFileSystemObject Members

        public string Name
        {
            get { return this.location; }
        }

        public DateTime LastModifiedTime
        {
            get { return System.IO.Directory.GetLastWriteTime(this.location); }
        }

        public Dictionary<string, string> Attributes
        {
            get 
            { throw new NotImplementedException(); }
        }

        public IFile AddFile(string fileName)
        {
            SimpleFS.Storage.Windows.File file = new File(fileName);
            return file;
        }

        public IDirectory AddDirectory(string directoryName)
        {
            SimpleFS.Storage.Windows.Directory directory 
                = new Directory(directoryName);
            return directory;
        }

        public IDirectory ParentDirectory
        {
            get
            {
                DirectoryInfo directoryInfo = System.IO.Directory.GetParent(this.location);
                Directory parentDirectory = new Directory(directoryInfo.Name);

                return parentDirectory;
            }
        }

        public void Create()
        {
            DirectoryInfo info = System.IO.Directory.CreateDirectory(this.Name);
            Console.WriteLine(info.Exists);
            Console.WriteLine(info.FullName);
        }

        public void Copy(IFileSystemObject target)
        {
            throw new NotImplementedException();
        }

        public void Move(IFileSystemObject target)
        {
            throw new NotImplementedException();
        }

        public void Close()
        {
            
        }

        #endregion

        #region IFileSystemObject Members


        public int Size
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
